<%@ page language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <%
		String roleId = request.getParameter("roleId");
		String baseUrl = request.getContextPath();
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
		$(document).ready(function(){
			// 全局参数
			var toolbar = new Ext.Toolbar({
		    	renderTo: 'editPerToolBarDiv',
		    	items: [
				    new Ext.Button({
					    id: 'editPer-save-button',
						text: '保存',
						iconCls: 'tick'
					}),
					new Ext.Button({
					    id: 'editPer-cancel-button',
						text: '取消',
						iconCls: 'cancel'
					})
				]
			});
			
			var loader = new Ext.tree.TreeLoader({
			    id:'ptree',
			    preloadChildren : true,  
			    baseParams: {roleId:'<%=roleId %>'},
			    dataUrl:'${pageContext.request.contextPath}/getPermission.action',
                listeners : {
					beforeload : function(treeLoader, node) {
						this.baseParams.parentId = node.id
						this.baseParams.text = node.text
						this.baseParams.leaf = node.leaf
					}
				}
                });
                
			var mytree = new Ext.tree.TreePanel({
                el : "container",
                animate:true,//以动画形式伸展,收缩子节点
                rootVisible:true,//是否显示根节点
		        labelWidth: 50,
		        border: false,
		        bodyStyle: 'background-color:transparent;',
                autoScroll : true,
                autoHeight : true,
                loader: loader,
                width : 350,
                lines : true
                });

            // 根节点
            var root = new Ext.tree.AsyncTreeNode({
            nodeType: 'async',
            id:'-1',
            text:'权限控制树',
            leaf:false
            // checked : false
            })
            
            mytree.setRootNode(root);
            mytree.render();
            mytree.on('checkchange', function(node, checked) {
                        node.expand();
                        node.attributes.checked = checked;
                        node.eachChild(function(child) {
                                    child.ui.toggleCheck(checked);
                                    child.attributes.checked = checked;
                                    child.fireEvent('checkchange', child,
                                            checked);
                               });
                    }, mytree);
                    
                    //自动展开所有子节点
                    mytree.expandAll(); 
                    
                    // 保存按钮
				    $("#editPer-save-button").click(function(){
				            var b = mytree.getChecked();
                            var checkid = new Array;// 存放选中id的数组
                            for (var i = 0; i < b.length; i++) {
                                checkid.push(b[i].id);// 添加id到数组
                            }
                            //alert(checkid.toString());
					    	// 发送请求
							Anynote.ajaxRequest({
								baseUrl: '<%=baseUrl %>',
								baseParams: {roleId:'<%=roleId %>', per_id:checkid.toString()},
								action: '${pageContext.request.contextPath}/updatePerInfor.action',
								callback: function(jsonResult){
								    roleListGrid.getStore().reload();
									editPerWindow.close();
								},
								showWaiting: true,
								failureMsg: '保存失败.'
							});
					});
					
				 	// 取消按钮
				    $("#editPer-cancel-button").click(function(){
				    	// Ext.getCmp("buttonType").setValue("cancel");
				    	editPerWindow.close();
					});
			});
	</script>
</head>
<body>
<div id="editPerToolBarDiv"></div>  
<div id="container" ></div>
<%-- 
<div id="btn" ></div> 
--%>
</body>
</html>

